﻿//-----------------------------------------------------------------------------
// <copyright file="OpenFileDialogImplementer.cs" company="William E. Kempf">
//     Copyright (c) William E. Kempf.
// </copyright>
//-----------------------------------------------------------------------------

namespace Onyx.Windows
{
    using System.IO;
    using System.Windows;
    using Microsoft.Win32;

    /// <summary>
    /// Provides a standard implementation of <see cref="IOpenFileDialog"/> that
    /// defers operations to a <see cref="Microsoft.Win32.OpenFileDialog"/>.
    /// </summary>
    public class OpenFileDialogImplementer : FileDialogImplementer<OpenFileDialog>, IOpenFileDialog
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="OpenFileDialogImplementer"/>
        /// class.
        /// </summary>
        /// <param name="owner">The owner <see cref="Window"/>.</param>
        public OpenFileDialogImplementer(Window owner)
            : base(owner, new OpenFileDialog())
        {
        }

        /// <summary>
        /// Gets or sets a value indicating whether <see cref="IOpenFileDialog"/> allows users to select multiple files.
        /// </summary>
        /// <value><see langword="true"/> if multiple selections are allowed; otherwise, <see langword="false"/>. The
        /// default value is <see langword="false"/>.</value>
        public bool Multiselect
        {
            get
            {
                return Dialog.Multiselect;
            }

            set
            {
                Dialog.Multiselect = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether the read-only check box displayed
        /// by <see cref="IOpenFileDialog"/> is selected.
        /// </summary>
        /// <value><see langword="true"/> if the checkbox is selected; otherwise, <see langword="false"/>.
        /// The default value is <see langword="false"/>.</value>
        public bool ReadOnlyChecked
        {
            get
            {
                return Dialog.ReadOnlyChecked;
            }

            set
            {
                Dialog.ReadOnlyChecked = value;
            }
        }

        /// <summary>
        /// Gets or sets a value indicating whether <see cref="IOpenFileDialog"/>
        /// contains a read-only check box.
        /// </summary>
        /// <value><see langword="true"/> if the checkbox is displayed; otherwise, <see langword="false"/>.
        /// The default value is <see langword="false"/>.</value>
        public bool ShowReadOnly
        {
            get
            {
                return Dialog.ShowReadOnly;
            }

            set
            {
                Dialog.ShowReadOnly = value;
            }
        }

        /// <summary>
        /// Opens a read-only stream for the file that is selected by the user using
        /// <see cref="IOpenFileDialog"/>.
        /// </summary>
        /// <returns>A new <see cref="System.IO.Stream"/> that contains the selected
        /// file.</returns>
        /// <exception cref="System.InvalidOperationException">No files were selected
        /// in the dialog.</exception>
        /// <remarks>The file is opened with read-only permission
        /// (<see cref="System.IO.FileMode.Open"/>, <see cref="System.IO.FileAccess.Read"/>,
        /// and <see cref="System.IO.FileShare.Read"/>), whether or not the Read Only
        /// checkbox is checked in the dialog. Because the file is opened without the
        /// ability to overwrite or write to the file, an application can safely load
        /// a file while executing within partial trust restrictions.</remarks>
        public Stream OpenFile()
        {
            return Dialog.OpenFile();
        }

        /// <summary>
        /// Creates an array that contains one read-only stream for each file selected
        /// by the user using <see cref="IOpenFileDialog"/>.
        /// </summary>
        /// <returns>An array of multiple new <see cref="System.IO.Stream"/> objects
        /// that contain the selected files.</returns>
        /// <exception cref="System.InvalidOperationException">No files were selected
        /// in the dialog.</exception>
        /// <remarks>Each file is opened with read-only permission
        /// (<see cref="System.IO.FileMode.Open"/>, <see cref="System.IO.FileAccess.Read"/>,
        /// and <see cref="System.IO.FileShare.Read"/>), whether or not the Read Only
        /// checkbox is checked in the dialog. Because each file is opened without
        /// the ability to overwrite or write to the file, an application can safely
        /// load one or more files while executing within partial trust restrictions.</remarks>
        public Stream[] OpenFiles()
        {
            return Dialog.OpenFiles();
        }
    }
}
